﻿using Authentication.Core.Configuration;
using Authentication.Model.Interface;
using Common.Contract.Authentication.Record;
using Common.Contract.Authentication.Request;
using Common.Contract.Authentication.Response;
using Common.Contract.Paging;
using Common.Contract.SystemConfig.Record;
using Common.Data.Repository.Enum;
using Common.Service.Command;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Authentication.Core.ServiceCommand
{
    public class RetrieveListCloseDaoRequestServiceCommand : ProviderServiceCommand<IProviderFactory, RetrieveListCloseDaoRequestRequest, RetrieveListCloseDaoRequestResponse>
    {

        public int pageSize = 10, pageIndex = 0, totalRecord = 0;
        public string TextSearch;
        public FilterRecord Filter = null;
        private UserRecord currentUser;
        public RetrieveListCloseDaoRequestServiceCommand(IProviderFactory providers) : base(providers) { }
        protected override void Validate(RetrieveListCloseDaoRequestRequest request)
        {
            if (request == null) throw new ArgumentNullException("request");
            if (!request.Header.ApplicationKey.HasValue || request.Header.ApplicationKey == Guid.Empty)
            {
                throw new ArgumentException("ApplicationKey must be set", "request");
            }
            if (request.CurrentUser == null)
            {
                throw new ArgumentException("User record must be set", "request");
            }
            else
            {
                currentUser = request.CurrentUser;
            }
            if (Request.CurrentUser == null)
            {
                throw new ArgumentException("Dao must be set", "request");
            }
            Filter = Request.Filter;
        }

        protected override bool OnExecute(RetrieveListCloseDaoRequestResponse response)
        {
            bool isSuccess = true;
            string errorMessage = string.Empty;
            int pageSize = 0, pageIndex = 0, totalRecord = 0;
            var applicationKey = Request.Header.ApplicationKey;
            try
            {
                if (Request.Page != null)
                {
                    pageIndex = Request.Page.PageIndex ?? 0;
                    pageSize = Request.Page.PageSize ?? 0;
                }
                var listUser = Providers.CloseDao.RetrieveListCloseDaoRequest(out totalRecord, currentUser, Filter, pageSize, pageIndex, Request.Sort);
                if (listUser == null)
                {
                    response.ErrorMessage = string.Format("Can not found user with Dao={0}", currentUser.Dao);
                    response.IsSuccess = false;
                }
                List<CloseDaoRequestRecord> records = listUser.Select(x => new CloseDaoRequestRecord
                {
                    Id = x.Id,
                    Pic = x.Pic,
                    Dao = x.Dao,
                    Zone = x.Branch == null ? string.Empty : x.Branch.Region,
                    Branch = x.Branch == null ? string.Empty : x.Branch.BranchNameSme,
                    CreatedDate = x.CreatedDate,
                    BranchId = x.Branch.BranchId,
                    Status = x.Status,
                    PathOfferLetter = string.IsNullOrEmpty(x.LinkOfferLetter) ? string.Empty : x.LinkOfferLetter,
                    RejectReason = string.IsNullOrEmpty(x.RejectReason) ? string.Empty : x.RejectReason,
                    RejectPerson = string.IsNullOrEmpty(x.RejectPerson) ? string.Empty : x.RejectPerson + " ",
                    NeedApprove = ((SystemConfiguration.PostionHeadBranch.Contains(currentUser.Position) || currentUser.GroupIdentifier.Equals("2")) && x.Status == (int)StatusCloseDaoEnum.Request),
                    CanReject = x.Status == (int)StatusCloseDaoEnum.Request || ((x.Status == (int)StatusCloseDaoEnum.BranchApproved || x.Status == (int)StatusCloseDaoEnum.RegionApproved) && SystemConfiguration.UserBICApprovedDao.Contains(currentUser.UserName)),
                    NeedUploadOfferLetter = string.IsNullOrEmpty(x.LinkOfferLetter) && x.Pic.Equals(currentUser.UserName) && x.Status == (int)StatusRequestCreateDaoEnum.Request,
                }).ToList();
                response.Records = records;
                response.Page = new PageRecord
                {
                    PageIndex = pageIndex,
                    PageSize = pageSize,
                    TotalRecords = totalRecord,
                };
            }
            catch (Exception e)
            {
                Console.Write(e.Message);
                response.ErrorMessage = string.Format("Can not found user with Dao={0}", Request.CurrentUser.Dao);
                response.IsSuccess = false;
            }
            return isSuccess;
        }
    }
}
